package com.starsoft.verifycode.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;


public class ImageIOHelper {
	
	private ImageIOHelper() {}

	public static BufferedImage read(File imageFile) {
		try {
			return ImageIO.read(imageFile);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static BufferedImage read(InputStream is){
		try {
			return ImageIO.read(is);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static File getResourceFile(String path){
		String absolutPath = ImageIOHelper.getResourcePath(path);
		return new File(absolutPath);
	}
	
	public static String getResourcePath(String path){
		if (!path.startsWith("/") && !path.startsWith("\\")) {
			path = "/" + path;
		}
		String absolutPath = ImageIOHelper.class.getResource(path).getFile();
		return absolutPath;
	}

}